package com.stage.item
{
	import com.resource.unit.ActionUnit;
	import com.resource.unit.BaseUnit;
	import com.utils.time.TimerClock;
	
	import flash.display.Sprite;
	import flash.media.Video;

	public class BasePlayer
	{

		
		public static const WALK_SPEED:Number = 15;
		public static const JUMP_SPEED:Number = 20;
		
		public static const WALK_DIR_NONE:int = 0;
		public static const WALK_DIR_LEFT:int = -1;
		public static const WALK_DIR_RIGHT:int = 1;
		
		protected var _view:BaseUnit = new BaseUnit;
		protected var _data:Object;
		protected var _container:Sprite;
		protected var _threadId:int;
		
		protected var _speed:Number = WALK_SPEED; // 每帧走动速度
		protected var _walkDir:int = 0; // 走动方向，不为0时一直走动
		protected var _jumpSpeed:Number = 0;
		protected var _targetX:Number = -1; // 小于0时表示没有目标位置
		protected var _targetY:Number = -1;
		protected var _isPaused:Boolean = false; // 走动暂停
		
		protected var _endCallbackList:Array = []; // 动作结束回调函数，函数形式为 f(BasePlayer)
		
		public function BasePlayer(data:Object, con:Sprite)
		{
			this._data = data;
			this._container = con;
			
			_view.setPart(BaseUnit.NUDE, "res/", loadComplete); // 不换装的情况下都用nude代表全身
			_view.endCallback = actionEnd;
		}
		
		private function loadComplete():void{
			trace("pre load complete");
		}
		
		private function actionEnd():void{
			for(var i:int=0; i<_endCallbackList.length; i++){
				var func:Function = _endCallbackList[i];
				if(func){
					func.apply(null, [this]);
				}
			}
		}
		
		/**
		 * 添加一个回调
		 * func的格式：func(player:Player):void
		 */
		public function addActionEndListener(func:Function):void{
			removeActionEndListener(func);
			_endCallbackList.push(func);
		}
		
		/**
		 * 删除一个回调
		 */
		public function removeActionEndListener(func:Function):void{
			for(var i:int=0; i<_endCallbackList.length; i++){
				if(_endCallbackList[i] == func){
					_endCallbackList.splice(i, 1);
				}
			}
		}
		/**
		 * 向目标位置寻路
		 */
		public function routeTo(tx:Number, ty:Number, speed:Number = WALK_SPEED):void{
			
		}
		
		/**
		 * 走到目标点
		 */
		public function walkTo(tx:Number, ty:Number, speed:Number = WALK_SPEED):void{
			_walkDir = dir;
			_targetX = tx;
			_targetY = ty;
			walkStart();
		}
		
		/**
		 * 往某个方向走动，直到被停止
		 * 一般是在用键盘操作时使用
		 * 注意：
		 * 1.是否需要支持8方向的走动
		 * 2.是否需要判断可走区域
		 */
		public function walkDir(dir:int, speed:Number = WALK_SPEED):void{
			_targetX = -1;
			_targetY = -1;
			_walkDir = dir;
			_speed = speed;
			walkStart();
		}
		
		/**
		 * 暂停走动
		 */
		public function walkPause():void{
			_isPaused = true;
		}
		
		/**
		 * 走动开始
		 */
		public function walkStart():void{
			_isPaused = false;
			if(_walkDir != WALK_DIR_NONE){
				dir = _walkDir;
			}else{
				if(_targetX > 0){
					if(_targetX > x){
						dir = WALK_DIR_RIGHT;
					}else if(_targetX < x){
						dir = WALK_DIR_LEFT;
					}
				}
			}
			act = ActionUnit.RUN;
		}
		
		/**
		 * 停止走动
		 */
		public function walkStop():void{
			_walkDir = 0;
			_targetX = -1;
			_targetY = -1;
			act = ActionUnit.STAND;
		}
		
		/**
		 * 帧监听事件
		 */
		private function enterFrame():void{
			// 处理动作
			_view.enterFrame();
			
			// 处理走动
			if(!_isPaused){
				if(_walkDir != 0){
					if(_walkDir == WALK_DIR_LEFT){
						x -= _speed;
					}else if(_walkDir == WALK_DIR_RIGHT){
						x += _speed;
					}
				}else if(_targetX >= 0 && _targetY >= 0){
					
				}
			}
		}
		
		/**
		 * 不同与动画类的播放
		 * 是对帧监听的控制
		 */
		public function play():void{
			_threadId = TimerClock.getInstance().addEnterFrame(enterFrame);
		}
		
		/**
		 * 不同与动画类的停止
		 * 是对帧监听的控制
		 */
		public function stop():void{
			TimerClock.getInstance().removeEnterFrame(_threadId);
		}
		
		public function set act(value:String):void{
			_view.act = value;
		}
		
		public function get act():String{
			return _view.act;
		}
		
		public function get x():Number{
			return _view.x;
		}
		
		public function set x(value:Number):void{
			_view.x = value;
		}
		
		public function get y():Number{
			return _view.y;
		}
		
		public function set y(value:Number):void{
			_view.y = value;
		}
		
		public function get dir():int{
			return _view.dir;
		}
		
		public function set dir(value:int):void{
			_view.dir = value;
		}
		
		public function get view():BaseUnit{
			return _view;
		}
		
		public function get data():Object{
			return _data;
		}
		
		public function get container():Sprite{
			return _container;
		}
		
		public function set container(value:Sprite):void{
			if(!_container){
				return;
			}
			if(visible){
				_container.removeChild(_view);
				_container = value;
				_container.addChild(_view);
			}else{
				_container = value;
			}
		}
		
		public function get visible():Boolean{
			return _container.contains(_view);
		}
		
		public function set visible(value:Boolean):void{
			if(value){
				_container.addChild(_view);
			}else{
				_container.removeChild(_view);
			}
		}
	}
}